﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace AutoTrader
{
    public partial class AutoTraderForm
    {
        private bool ReorderStopOrder()
        {
            int errNumber = 0;
            string BuySell = string.Empty;
            int StopID = StopOrder.ID;
            decimal StopPrice = StopOrder.Price;

            try
            {
                FailedCount = 0;

                if (StopOrder.BuyOrSell == "1")
                {
                    StopOrder.Price = BuyPrice - PipsStep;//StopOrder.Price + PipsStep + PipsStep*0.2m;                   
                    BuySell = "S";
                }
                else
                {
                    StopOrder.Price = SellPrice + PipsStep; ;//StopOrder.Price - PipsStep - PipsStep*0.2m;
                    BuySell = "B";
                }

                myLogger.Add(string.Format("Try Open Stop Order: Instrument:{0}, InitialLots:{1}, Action:{2}, Validity:{3}, ",
                                 Settings.Settings.Instrument, InitialLots.ToString(), BuySell, 1 ) );

                StopOrder.ID = obj.NewOrder(Settings.Settings.Instrument, InitialLots*1000, (double)StopOrder.Price, 0, 3, BuySell, "2");
                if (StopOrder.ID < 0)
                {
                    myLogger.Add(string.Format("New Stop Order Error while reorder.{0}", obj.GetErrDescription(StopOrder.ID)));
                    StopOrder.ID = 0;
                    return false;
                }

                string message = obj.GetOrdParams(StopOrder.ID, ref errNumber);

                if (errNumber < 0)
                {
                    myLogger.Add(string.Format("New Stop Order Error.{0}", obj.GetErrDescription(errNumber)));
                    return false;
                }
                myLogger.Add(string.Format("New Stop Order Opened: {0} - {1}", StopOrder.ID.ToString(), message));

                errNumber = obj.CancelOrder(StopID);// cancel old stop order
                if (errNumber < 0)
                {
                    myLogger.Add(string.Format("Cancel Stop Order Error while reorder.{0}", obj.GetErrDescription(errNumber)));
                    //return false;
                }
                myLogger.Add(string.Format("Old Stop Order Canceled.{0} at {1}) ", StopID.ToString(), StopPrice.ToString()));

                dResultAll = obj.GetTotalResult(Settings.Settings.Instrument.Substring(4, 3), ref errNumber);
                if (errNumber < 0)
                    myLogger.Add(string.Format("Get Total Result Error.{0}", obj.GetErrDescription(errNumber)));
                else
                    myLogger.Add(string.Format("Current Result: {0} {1}", dResultAll.ToString("F3", CultureInfo.InvariantCulture), Settings.Settings.Instrument.Substring(4, 3)));

                WriteOrderSettings();
            }
            catch (Exception ex)
            {
                myLogger.Add(string.Format("ReorderStopOrder : Runtime exception: {0}-{1}", ex.Message, ex.StackTrace));
                return false;
            }

            return true;
        }
    }
}
